import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import path from 'path'
// import vueJsx from '@vitejs/plugin-vue-jsx'
// import VueDevTools from 'vite-plugin-vue-devtools'
import monacoEditorPlugin from 'vite-plugin-monaco-editor'
import svgLoader from 'vite-svg-loader'

export default defineConfig({
  plugins: [
    vue(),
    svgLoader(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
      imports: ['vue', 'vue-router', 'pinia']
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    }),
    // @ts-ignore
    monacoEditorPlugin.default({})
  ],
  define: {
    global: 'self',
    'process.env': {}
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    fs: {
      allow: ['..']
    },
    headers: {
      'Cross-Origin-Opener-Policy': 'same-origin',
      'Cross-Origin-Embedder-Policy': 'require-corp',
    }
  },
  build: {
    target: 'esnext',
    rollupOptions: {
      output: {
        manualChunks: {
          'monaco-editor': ['monaco-editor'],
          'vue-flow': ['@vue-flow/core', '@vue-flow/minimap', '@vue-flow/controls'],
          'element-plus': ['element-plus'],
          'parsers': ['fast-xml-parser', 'libxml2-wasm']
        }
      }
    }
  },
  optimizeDeps: {
    esbuildOptions: {
      target: 'esnext'
    },
    include: [
      'vue',
      'pinia',
      '@vueuse/core',
      'element-plus',
      'dagre',
      '@vue-flow/core',
      '@vue-flow/minimap',
      '@vue-flow/controls',
      '@vue-flow/node-resizer',
      '@vue-flow/node-toolbar',
      'monaco-editor',
      'xml-formatter',
      'js-yaml',
      '@iarna/toml',
      'fast-xml-parser',
      'libxml2-wasm'
    ],
    needsInterop: [
      '@swc/wasm-web'
    ],
    exclude: [
      '@swc/wasm-web',
      'quickjs-emscripten'
    ]
  },
})